package com.mkz.producer.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mkz.core.common.util.ResponseResult;
import com.mkz.core.log.annotation.SysLog;
import com.mkz.producer.entity.MsgLog;
import com.mkz.producer.service.MsgLogService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;


/**
 * 消息日志记录
 *
 * @author KuangJianyu
 * @date 2020-04-16 13:19:44
 */
@RestController
@AllArgsConstructor
@RequestMapping("/msglog" )
@Api(value = "msglog", tags = "消息日志记录管理")
public class MsgLogController {

    private final  MsgLogService msgLogService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param msgLog 消息日志记录
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public ResponseResult getMsgLogPage(Page page, MsgLog msgLog) {
    	if(null==msgLog) {
    		msgLog = new MsgLog();
    	}
    	msgLog.setDelFlag("1");
        return ResponseResult.ok(msgLogService.page(page, Wrappers.query(msgLog)));
    }


    /**
     * 通过id查询消息日志记录
     * @param id id
     * @return ResponseResult
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public ResponseResult getById(@PathVariable("id" ) Long id) {
        return ResponseResult.ok(msgLogService.getById(id));
    }

    /**
     * 新增消息日志记录
     * @param msgLog 消息日志记录
     * @return ResponseResult
     */
    @ApiOperation(value = "新增消息日志记录", notes = "新增消息日志记录")
    @SysLog("新增消息日志记录" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('msmq_msglog_add')" )
    public ResponseResult save(@RequestBody MsgLog msgLog) {
        return ResponseResult.ok(msgLogService.save(msgLog));
    }

    /**
     * 修改消息日志记录
     * @param msgLog 消息日志记录
     * @return ResponseResult
     */
    @ApiOperation(value = "修改消息日志记录", notes = "修改消息日志记录")
    @SysLog("修改消息日志记录" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('msmq_msglog_edit')" )
    public ResponseResult updateById(@RequestBody MsgLog msgLog) {
        return ResponseResult.ok(msgLogService.updateById(msgLog));
    }

    /**
     * 通过id删除消息日志记录
     * @param id id
     * @return ResponseResult
     */
    @ApiOperation(value = "通过id删除消息日志记录", notes = "通过id删除消息日志记录")
    @SysLog("通过id删除消息日志记录" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('msmq_msglog_del')" )
    public ResponseResult removeById(@PathVariable Long id) {
    	MsgLog msgLog = msgLogService.getById(id);
    	msgLog.setDelFlag("0");
        return ResponseResult.ok(msgLogService.updateById(msgLog));
    }

}
